package com.shenmazong.controller;

import com.shenmazong.service.TbSellerService;
import com.shenmazong.utils.FileUploadUtil;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/1/8 11:32
 */

@RestController
@Slf4j
@Api(tags = {"卖家接口"})
@RequestMapping(value = "/seller")
public class TbSellerUserController {

    @Autowired
    TbSellerService tbSellerService;


    @ApiOperation(value="买家用户登录", notes="根据用户名和密码进行登录", produces="application/json")
    @PostMapping(value = "/userLogin")
    public ResultResponse userLogin(@RequestBody LoginVo loginVo) {
        return tbSellerService.userLogin(loginVo);
    }

    @ApiOperation(value="买家用户重置密码", notes="根据原密码对密码进行重置", produces="application/json")
    @PostMapping(value = "/userResetPassword")
    public ResultResponse userResetPassword(@RequestBody ResetPassworVo resetPassworVo) {
        return tbSellerService.userResetPassword(resetPassworVo);
    }

    @ApiOperation(value="买家用户注册", notes="买家用户进行注册", produces="application/json")
    @PostMapping(value = "/addUser")
    public ResultResponse addUser(@RequestBody AddSellerVo addSellerVo) {
        return tbSellerService.addUser(addSellerVo);
    }

    @ApiOperation(value="发送验证码", notes="根据手机号发送验证码", produces="application/json")
    @PostMapping(value = "/sendCode")
    public ResultResponse sendCode(@RequestBody MobileVo mobileVo) {
        return tbSellerService.sendCode(mobileVo);
    }

    @ApiOperation(value="获取卖家信息", notes="获取卖家信息", produces="application/json")
    @PostMapping(value = "/getSellerInfo")
    public ResultResponse getSellerInfo(@RequestBody IdVo idVo) {
        return tbSellerService.getSellerInfo(idVo);
    }

    @ApiOperation(value="修改商家信息", notes="修改商家信息", produces="application/json")
    @PostMapping(value = "/setSellerInfo")
    public ResultResponse setSellerInfo(@RequestBody SellerInfoVo sellerInfoVo) {
        return tbSellerService.setSellerInfo(sellerInfoVo);
    }

    @ApiOperation(value="商家上传LOGO", notes="商家上传LOGO", produces="application/json")
    @PostMapping("/logoUpload")
    public ResultResponse logoUpload(@RequestParam("file") MultipartFile file){
        // 设置文件上传配置
        FileUploadUtil uploadUtil = FileUploadUtil.upload()
                .setDomain("http://localhost:8040/user/img/")
                .setUploadDir("E:/var/images");

        // 文件上传
        String fileUrl = uploadUtil.saveFile(file);
        if(fileUrl == null) {
            return ResultResponse.FAILED(505, "文件上传失败");
        }

        // 获取上传文件信息
        HashMap<String, String> map = new HashMap<>();
        map.put("name", uploadUtil.getFileName());
        map.put("url", uploadUtil.getFileUrl());

        return ResultResponse.SUCCESS(map);
    }
}
